<?php
/**
 * 功能：公共Controller
 * 
 * 
 */
class IndexAction extends CommonAction {
    public function index(){
    	layout('Layout/index'); //布局文件Tpl/Layout/index.html

        $login_id = $this->login_id; //当前登录用户ID

        //显示菜单项
        $menu  = array();
        if(isset($_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]])) {
            //如果已经缓存，直接读取缓存
            $menu = $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]];
        }else {            
            $_group = M("Group");
            $uid = $_group->getField("uid");

            $where['uid'] = $uid;
            //select 返回：失败false 空NUll 成功array(二维索引)
            $list = $_group->where($where)->field('gid,group_name,uid,create_time')->order('gid asc')->select();

            $menu = empty($list) ? array() : $list;

            //缓存菜单访问
            $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]] = $menu;
        }

        //分组记录条数
        //SELECT gid,COUNT(id) AS num  FROM card_info_group WHERE uid=2 GROUP BY gid ORDER BY gid
        $_info_group = M('Info_group');
        $arr_num = $_info_group->where("uid='{$login_id}'")->field("gid,COUNT(id) AS num")->group("gid")->order("gid")->select();
        
        $group_num = array();
        if(!empty($arr_num)){
            foreach ($arr_num as $k => $v) {
                $group_num[$v['gid']] = $v['num'];
            }
        }
        
        $total_num = array_sum($group_num); //全部

        $this->assign('menu', $menu); //菜单
        $this->assign('group_num', $group_num);
        $this->assign('total_num', $total_num);
		$this->display();
    }

    //菜单
    function menu(){
        //显示菜单项
        $menu  = array();
        if(isset($_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]])) {
            //如果已经缓存，直接读取缓存
            $menu = $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]];
        }else {            
            $_group = M("Group");
            $uid = $_group->getField("uid");

            $where['uid'] = $uid;
            //select 返回：失败false 空NUll 成功array(二维索引)
            $list = $_group->where($where)->field('gid,group_name,uid,create_time')->order('gid asc')->select();

            $menu = empty($list) ? array() : $list;

            //缓存菜单访问
            $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]] = $menu;
        }

        if(!empty($_GET['tag'])){
            $this->assign('menuTag',$_GET['tag']);
        }

        $this->assign('menu', $menu);
        $this->display();
    }

    
}